// import JSEncrypt from 'jsencrypt'
const Encrypt = require('../utils/jsencrypt.js')
// 字符串转base64
export function base64_encode(str) {
  if (typeof str !== 'string') str = String(str)
  var c1, c2, c3
  var base64EncodeChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  var i = 0,
    len = str.length,
    string = ''

  while (i < len) {
    c1 = str.charCodeAt(i++) & 0xff
    if (i == len) {
      string += base64EncodeChars.charAt(c1 >> 2)
      string += base64EncodeChars.charAt((c1 & 0x3) << 4)
      string += '=='
      break
    }
    c2 = str.charCodeAt(i++)
    if (i == len) {
      string += base64EncodeChars.charAt(c1 >> 2)
      string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4))
      string += base64EncodeChars.charAt((c2 & 0xf) << 2)
      string += '='
      break
    }
    c3 = str.charCodeAt(i++)
    string += base64EncodeChars.charAt(c1 >> 2)
    string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4))
    string += base64EncodeChars.charAt(((c2 & 0xf) << 2) | ((c3 & 0xc0) >> 6))
    string += base64EncodeChars.charAt(c3 & 0x3f)
  }
  return string
}

// 密钥对生成 http://web.chacuo.net/netrsakeypair

const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANL378k3RiZHWx5AfJqdH9xRNBmD9wGD\n' + '2iRe41HdTNF8RUhNnHit5NpMNtGL0NPTSSpPjjI1kJfVorRvaQerUgkCAwEAAQ=='

const privateKey = 'MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEA0vfvyTdGJkdbHkB8\n' + 'mp0f3FE0GYP3AYPaJF7jUd1M0XxFSE2ceK3k2kw20YvQ09NJKk+OMjWQl9WitG9p\n' + 'B6tSCQIDAQABAkA2SimBrWC2/wvauBuYqjCFwLvYiRYqZKThUS3MZlebXJiLB+Ue\n' + '/gUifAAKIg1avttUZsHBHrop4qfJCwAI0+YRAiEA+W3NK/RaXtnRqmoUUkb59zsZ\n' + 'UBLpvZgQPfj1MhyHDz0CIQDYhsAhPJ3mgS64NbUZmGWuuNKp5coY2GIj/zYDMJp6\n' + 'vQIgUueLFXv/eZ1ekgz2Oi67MNCk5jeTF2BurZqNLR3MSmUCIFT3Q6uHMtsB9Eha\n' + '4u7hS31tj1UWE+D+ADzp59MGnoftAiBeHT7gDMuqeJHPL4b+kC+gzV4FGTfhR9q3\n' + 'tTbklZkD2A=='

// 加密
export function encrypt(txt) {
  // const encryptor = new JSEncrypt()
  // encryptor.setPublicKey(publicKey) // 设置公钥
  // return encryptor.encrypt(txt) // 对需要加密的数据进行加密
  const encryptor = new Encrypt.JSEncrypt()
  encryptor.setPublicKey(publicKey) // 设置公钥
  return encryptor.encrypt(txt) // 对需要加密的数据进行加密
}

// 解密
export function decrypt(txt) {
  // const encryptor = new JSEncrypt()
  // encryptor.setPrivateKey(privateKey)
  // return encryptor.decrypt(txt)
}

//获取当前周开始日期
export function getCurrentWeekFirstDay(date) {
  let weekFirstDay = new Date(date - (date.getDay() - 1) * 86400000)
  let firstMonth = Number(weekFirstDay.getMonth()) + 1

  if (firstMonth < 10) {
    firstMonth = '0' + firstMonth
  }
  let weekFirstDays = weekFirstDay.getDate()
  if (weekFirstDays < 10) {
    weekFirstDays = '0' + weekFirstDays
  }
  return weekFirstDay.getFullYear() + '-' + firstMonth + '-' + weekFirstDays
}
//获取当前周结束日期
export function getCurrentWeekLastDay(date) {
  let weekFirstDay = new Date(date - (date.getDay() - 1) * 86400000)
  let weekLastDay = new Date((weekFirstDay / 1000 + 6 * 86400) * 1000)
  let lastMonth = Number(weekLastDay.getMonth()) + 1
  if (lastMonth < 10) {
    lastMonth = '0' + lastMonth
  }
  let weekLastDays = weekLastDay.getDate()
  if (weekLastDays < 10) {
    weekLastDays = '0' + weekLastDays
  }
  return weekFirstDay.getFullYear() + '-' + lastMonth + '-' + weekLastDays
}
//获取当月起始时间
export function getMonth(type, months) {
  var d = new Date()
  var year = d.getFullYear()
  var month = d.getMonth() + 1
  if (Math.abs(months) > 12) {
    months = months % 12
  }
  if (months != 0) {
    if (month + months > 12) {
      year++
      month = (month + months) % 12
    } else if (month + months < 1) {
      year--
      month = 12 + month + months
    } else {
      month = month + months
    }
  }
  month = month < 10 ? '0' + month : month
  var date = d.getDate()
  var firstday = year + '-' + month + '-' + '01'
  var lastday = ''
  if (month == '01' || month == '03' || month == '05' || month == '07' || month == '08' || month == '10' || month == '12') {
    lastday = year + '-' + month + '-' + 31
  } else if (month == '02') {
    if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
      lastday = year + '-' + month + '-' + 29
    } else {
      lastday = year + '-' + month + '-' + 28
    }
  } else {
    lastday = year + '-' + month + '-' + 30
  }
  var day = ''
  if (type == 's') {
    day = firstday
  } else {
    day = lastday
  }
  return day
}
export function maskBankCard(cardNumber) {
  if (!cardNumber || cardNumber.length < 8) return cardNumber

  // 保留前4位和后4位，中间用*代替
  // const firstFour = cardNumber.substring(0, 4);
  const lastFour = cardNumber.substring(cardNumber.length - 4)
  const stars = ''

  return ` ${stars} ${lastFour}`
}
